package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionServiceImpl;

    @RequiresPermissions(value = {"permission:list", "权限列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Permission> result = permissionServiceImpl.query(qo);
        model.addAttribute("result", result);
        return "/permission/list";
    }


    @RequestMapping("/delete")
    @RequiresPermissions(value = {"permission:delete", "权限删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        permissionServiceImpl.delete(id);
        return new JsonResult("删除成功", true);
    }

    @RequestMapping("/reload")
    @RequiresPermissions(value = {"permission:reload", "权限加载"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult reload() {
        permissionServiceImpl.reload();
        return new JsonResult("加载成功", true);
    }
}
